"use strict"

var Router = require('koa-router');
var config = require('../../../config/config.js');
var dao = require('../../../dao/mongo/district.js');
var debug = require('debug')('district');

console.log('router:' + config.baseUrl + '/v1/district');
var router = new Router({
    prefix: config.baseUrl + '/v1/district'
});
router.get('/test', function*() {
    this.body = 'test work fine';
});

/**
 {
    "_id": "14800",
    "ad_code": "450102",
    "area_code": "14800",
    "city_code": "0771",
    "name": "华强",
    "center": ["108.312827", "22.821305"],
    "pid": "450102",
    "subid": [],
    "level": "biz_area"
}

 */
router.post('/', function*() {
    debug(this.request.inParams);
    var result = yield dao.save(this.request.inParams);
    this.body = result;
});


router.post('/batch', function*() {
    debug('/batch', this.request.inParams);
    var result = yield dao.saveMany(this.request.inParams);
    this.body = result;
});

router.get('/', function*() {
    var inParams = this.request.inParams;
    var result;
    if (inParams.id) {
        result = yield dao.findById(inParams.id);
    } else if (inParams.pid) {
        if (inParams.pid === 'null') {
            inParams.pid = null;
        }
        result = yield dao.findByPid(inParams.pid);
    }
    this.body = result;
});

module.exports = router.routes();